package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.*;
import com.ruoyi.business.domain.vo.CommonReviewVO;
import com.ruoyi.business.service.*;
import com.ruoyi.business.weapp.CommonContants;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.WxCompanyMapper;

/**
 * 公司认证管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-11-19
 */
@Service
public class WxCompanyServiceImpl implements IWxCompanyService 
{
    @Autowired
    private WxCompanyMapper wxCompanyMapper;

    @Autowired
    private IWxUserService wxUserService;

    @Autowired
    private ICommonReviewInfoService commonReviewInfoService;

    @Autowired
    private IMessageCenterService messageCenterService;

    @Autowired
    private IOperationRemindService operationRemindService;

    /**
     * 查询公司认证管理
     * 
     * @param companyId 公司认证管理主键
     * @return 公司认证管理
     */
    @Override
    public WxCompany selectWxCompanyByCompanyId(Long companyId)
    {
        return wxCompanyMapper.selectWxCompanyByCompanyId(companyId);
    }

    /**
     * 查询公司认证管理列表
     * 
     * @param wxCompany 公司认证管理
     * @return 公司认证管理
     */
    @Override
    public List<WxCompany> selectWxCompanyList(WxCompany wxCompany)
    {
        return wxCompanyMapper.selectWxCompanyList(wxCompany);
    }

    /**
     * 新增公司认证管理
     * 
     * @param wxCompany 公司认证管理
     * @return 结果
     */
    @Override
    public int insertWxCompany(WxCompany wxCompany)
    {
        wxCompany.setCreateTime(DateUtils.getNowDate());
        return wxCompanyMapper.insertWxCompany(wxCompany);
    }

    /**
     * 修改公司认证管理
     * 
     * @param wxCompany 公司认证管理
     * @return 结果
     */
    @Override
    public int updateWxCompany(WxCompany wxCompany)
    {
        wxCompany.setUpdateTime(DateUtils.getNowDate());
        return wxCompanyMapper.updateWxCompany(wxCompany);
    }

    /**
     * 批量删除公司认证管理
     * 
     * @param companyIds 需要删除的公司认证管理主键
     * @return 结果
     */
    @Override
    public int deleteWxCompanyByCompanyIds(Long[] companyIds)
    {
        return wxCompanyMapper.deleteWxCompanyByCompanyIds(companyIds);
    }

    /**
     * 删除公司认证管理信息
     * 
     * @param companyId 公司认证管理主键
     * @return 结果
     */
    @Override
    public int deleteWxCompanyByCompanyId(Long companyId)
    {
        return wxCompanyMapper.deleteWxCompanyByCompanyId(companyId);
    }

    @Override
    public int applyCompanyAuthInfo(WxCompany wxCompany) {
        WxUser wxUser = wxUserService.selectWxUserByUserId(wxCompany.getUserId());
        if (wxUser == null) {
            return 0;
        }
        wxCompany.setStatus(CommonContants.AUTHING);
        int i = insertWxCompany(wxCompany);
        wxUser.setCompanyAuthStatus(CommonContants.AUTHING);
        wxUser.setCompanyAuthStatusDes("公司认证中");
        wxUser.setBindingCompanyId(wxCompany.getCompanyId());
        wxUser.setBindingCompanyName(wxCompany.getName());
        int updateWxUser = wxUserService.updateWxUser(wxUser);
        return i & updateWxUser;
    }

    @Override
    public int reviewCompanyAuthInfo(CommonReviewVO commonReviewVO) {
        WxUser wxUser = wxUserService.selectWxUserByUserId(commonReviewVO.getUserId());
        if (wxUser == null) {
            return 0;
        }
        wxUser.setCompanyAuthStatus(commonReviewVO.getStatus());
        wxUser.setOwnerAuthStatusDes(CommonContants.STATUS_DES_MAP.get(commonReviewVO.getStatus()));

        WxCompany wxCompany = wxCompanyMapper.selectWxCompanyByCompanyId(commonReviewVO.getRecordId());
        if (wxCompany == null) {
            return 0;
        }
        wxCompany.setStatus(commonReviewVO.getStatus());
        wxCompany.setRemark(commonReviewVO.getContent());

        CommonReviewInfo commonReviewInfo = new CommonReviewInfo();
        commonReviewInfo.setReviewContent(commonReviewVO.getContent());
        commonReviewInfo.setDestStatus(commonReviewVO.getStatus());
        commonReviewInfo.setOriginalStatus(wxCompany.getStatus());

        MessageCenter messageCenter = new MessageCenter();
        messageCenter.setTitle("公司认证审核结果");
        String content = "公司认证审核通过,感谢您的使用";
        if (commonReviewVO.getStatus().equals("3")) {
            content = "审核被驳回,驳回理由：" + commonReviewVO.getContent();
        } else if (commonReviewVO.getStatus().equals("4")) {
            content = "认证已被废弃,废弃理由：" + commonReviewVO.getContent();
        }

        OperationRemind reflash_login_inf = new OperationRemind("REFLASH_LOGIN_INFO",
                "[公司认证]" + content, wxCompany.getCompanyId(), "0",
                wxUser.getUserId(), null, DateUtils.getNowDate(), wxUser.getUserId().toString());
        operationRemindService.insertOperationRemind(reflash_login_inf);

        messageCenter.setContent("[公司认证]" + content);
        messageCenter.setIsRead("0");
        messageCenter.setModule("公司认证");
        messageCenter.setDestUserId(commonReviewVO.getUserId().toString());
        messageCenterService.insertMessageCenter(messageCenter);

        return wxUserService.updateWxUser(wxUser) & wxCompanyMapper.updateWxCompany(wxCompany) & commonReviewInfoService.insertCommonReviewInfo(commonReviewInfo);
    }

    @Override
    public WxCompany selectWxCompanyByWxUserId(Long wxUserId) {
        return wxCompanyMapper.selectWxCompanyByWxUserId(wxUserId);
    }
}
